layui.use(['table', 'admin', 'ax','simTool','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var simtool = layui.simTool;
    var laydate = layui.laydate;

    /**
     * 会员用户管理
     */
    var Player = {
        tableId: "playerTable"
    };

    /**
     * 初始化表格的列
     */
    Player.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'id'},
            {field: 'userNo',align:'center', sort: true, title: '用户编号'},
            {field: 'nickname', sort: true,align:'center', title: '微信别名'},
            {field: 'userhead', sort: true,align:'center',minwidth:50, title: '头像',
                templet: "#pic_big"
            },
            {field: 'openid', sort: true,align:'center', hide:'true',  title: 'OPENID'},
            {field: 'phone', sort: true,align:'center', title: '电话'},
            {field: 'qrcode', sort: true,align:'center',hide:'true', title: '二维码入口'},
            {field: 'score', sort: true,align:'center', title: '积分'},
            {field: 'historyTotalScore', align:'center',sort: true,hide:'true', title: '历史总获得积分'},
            {field: 'deptid', sort: true,align:'center', hide:'true',title: '部门id'},
            {field: 'agentName', sort: true,align:'center', title: '代理商'},
            {field: 'sex', sort: true,align:'center', title: '性别',templet: "#sexTpl"},
            {field: 'introName', sort: true,align:'center', title: '推荐人'},
            {field: 'testAttendCount', sort: true,align:'center', title: '参与众测次数'},
            {field: 'selectedCount', sort: true,align:'center', title: '入选此数'},
            {field: 'uploadReportCount', sort: true,align:'center', title: '完成报告数'},
            {field: 'activityAttendCount', sort: true,align:'center', title: '活动参与次数'},
            {field: 'status',align:'center', sort: true, title: '状态',
                templet:'#statusTpl'
            },
            {field: 'deptName',align:'center', sort: true, title: '所属部门'},
            {field: 'lastLogin',align:'center', sort: true, title: '最后登录时间'},
            {field: 'createTime',align:'center', sort: true, title: '注册时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };


    /**
     *
     */
    var sim = new simtool("","");
    sim.updateButton("/player/editItem",Player.tableId,"status",[1,2])

    var  createTimeMin =null;
    var  createTimeMax =null;
    /**
     * 点击查询按钮
     */
    Player.search = function () {
        var queryData = {};
        queryData['userNo'] = $("#userNo").val();
        queryData['nickname'] = $("#nickname").val();
        queryData['introNickname'] = $("#introNickname").val();
        queryData['introUserNo'] = $("#introducerNo").val();
        queryData['deptid'] = $("#deptid").val();
        Player.handleData();
        queryData['createTimeMin'] = createTimeMin;
        queryData['createTimeMax'] = createTimeMax;
        table.reload(Player.tableId, {where: queryData,
            page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
    Player.handleData=function ( ) {
        createTimeMax = $("#createTimeMax").val();
        createTimeMin = $("#createTimeMin").val();
        if(!((createTimeMax==""&&createTimeMin=="")||(createTimeMax!="" && createTimeMin!=""))){
            if(createTimeMin==""){
                createTimeMin="2000-01-01"
            }
            if(createTimeMax==""){
                createTimeMax=Feng.currentDate();
            }
        }

    }
    //查看大图
    layer.photos({
        photos:"#photo_big"
        ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    });
    /**
     * 弹出添加对话框
     */
    Player.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加会员用户',
            content: Feng.ctxPath + '/player/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Player.tableId);
            }
        });
    };
    Player.openRestDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '会员余额信息',
            content: Feng.ctxPath + '/balanceHistory/?playerId=' + data.playerId,
            end: function () {
                admin.getTempData('formOk') && table.reload(Player.tableId);
            }
        });
    };


    /**
     * 导出excel按钮
     */
    Player.exportExcel = function () {
        var checkRows = table.checkStatus(Player.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            var a=[["1","启用"]]
            for(var i=0;i<checkRows.data.length;i++){
                var value = Feng.MatchStatus(a, checkRows.data[i].status,"未知");
                checkRows.data[i].status=value
            }

            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Player.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改会员用户',
            content: Feng.ctxPath + '/player/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Player.tableId);
            }
        });
    };


    laydate.render({
        elem: '#createTimeMin',
        max: Feng.currentDate()
    });

    laydate.render({
        elem: '#createTimeMax',
        max: Feng.currentDate()
    });




    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Player.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/player/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Player.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Player.tableId,
        url: Feng.ctxPath + '/player/list',
        page: true,
        width:2200,
        height: "full-58",
        cellMinWidth: 100,
        cols: Player.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Player.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Player.openAddDlg();
    });
    $('#rest').click(function () {
        Player.openRestDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Player.exportExcel();
    });


    // 工具条点击事件
    table.on('tool(' + Player.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Player.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Player.onDeleteItem(data);
        }
    });
});
